package jp.bsws.action.master;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.RaitenYoinBean;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.BSWSDao;
import jp.bsws.dao.RaitenYoinDao;
import jp.bsws.model.RaitenYoinModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/*
 * メインメニューアクションクラス
 */
public class RaitenYoinMasterAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;

	public String getLoginJugyoinKnj() { return bSWSSession.getStrJugyoinKnj(); }

	private List<RaitenYoinBean> raitenYoinBeanList;

	private Integer intTaisyoSeq;

	public void validateUpdateRaitenYoin() {

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		//必須チェック
		for(int i =0; i < raitenYoinBeanList.size(); i++){
			RaitenYoinBean raitenYoinBean = raitenYoinBeanList.get(i);
			if(intTaisyoSeq == raitenYoinBean.getSeq() && raitenYoinBean.isChkEdit()){
				if (BSWSUtils.isBlankNull(raitenYoinBean.getStrRaitenYoinKnj())) {
					addFieldError("raitenYoinBeanList[" + i + "].strRaitenYoinKnj", (i + 1) + "行目の来店要因名称を入力して下さい。");
				}
				break;
			}
		}
	}

	private void init() throws Exception{
		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);
	}

	@Override
	public String execute() throws Exception{

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//画面表示内容の設定
			hyojiJunbi(con);
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	/**
	 * 来店要因追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String add() throws Exception{

		init();

		for(RaitenYoinBean raitenYoinBean : this.raitenYoinBeanList) {
			raitenYoinBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			raitenYoinBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		}

		//来店要因行追加
		RaitenYoinBean raitenYoinBean = new RaitenYoinBean();
		raitenYoinBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		raitenYoinBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		this.raitenYoinBeanList.add(raitenYoinBean);

		return SUCCESS;
	}

	public String updateRaitenYoin() throws Exception{

		RaitenYoinDao raitenYoinDao = RaitenYoinDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{

			//来店要因情報更新対象取得
			for(RaitenYoinBean raitenYoinBean : raitenYoinBeanList) {
				int cnt = 0;
				if(raitenYoinBean.getSeq() != null){
					//来店要因情報更新
					cnt = raitenYoinDao.updateRaitenYoin(con, bSWSSession, raitenYoinBean);
				}else{
					if(BSWSUtils.isNotBlankNull(raitenYoinBean.getStrRaitenYoinKnj())
							&& BSWSUtils.isNotBlankNull(raitenYoinBean.getStrRaitenYoinRyakuKnj())
							&& BSWSUtils.isNotBlankNull(raitenYoinBean.getStrRaitenYoinHyojiJun())
							&& BSWSUtils.isNotBlankNull(raitenYoinBean.getStrRaitenYoinHyojiFlg())
					){
						//来店要因情報更新
						cnt = raitenYoinDao.insertRaitenYoin(con, null, bSWSSession, raitenYoinBean);
					}
				}
				if(cnt == 0){
					//処理なし
				}
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	public String deleteRaitenYoin() throws Exception{

		BSWSDao bSWSDao = BSWSDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//来店要因削除
			int cnt = bSWSDao.deleteRow(con, intTaisyoSeq, " M_RAITEN_YOIN ");
			if(cnt == 0){
				//処理なし
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	private void hyojiJunbi(Connection con) throws Exception{

		//来店要因マスタ情報を取得
		getRaitenYoinJoho(con);

		//来店要因行追加
		RaitenYoinBean raitenYoinBean = new RaitenYoinBean();
		raitenYoinBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		raitenYoinBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		raitenYoinBeanList.add(raitenYoinBean);
	}

	private void getRaitenYoinJoho(Connection con) throws Exception{

		RaitenYoinDao raitenYoinDao = RaitenYoinDao.getInstance();
		this.raitenYoinBeanList = new ArrayList<RaitenYoinBean>();
		this.raitenYoinBeanList.clear();

		//来店要因マスタ情報を取得
		List<RaitenYoinModel> raitenYoinList = raitenYoinDao.getRaitenYoinJoho(con, null, bSWSSession.getStrGroupCod(), null);
		for(int i = 0; i < raitenYoinList.size(); i++){
			RaitenYoinBean raitenYoinBean = new RaitenYoinBean();
			raitenYoinBean.setSeq(raitenYoinList.get(i).getSeq());
			raitenYoinBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			raitenYoinBean.setStrRaitenYoinKnj(raitenYoinList.get(i).getStrRaitenYoinKnj());
			raitenYoinBean.setStrRaitenYoinRyakuKnj(raitenYoinList.get(i).getStrRaitenYoinRyakuKnj());
			raitenYoinBean.setStrRaitenYoinHyojiJun(String.valueOf(raitenYoinList.get(i).getIntRaitenYoinHyojiJun()));
			raitenYoinBean.setStrRaitenYoinHyojiFlg(raitenYoinList.get(i).getStrRaitenYoinHyojiFlg());
			raitenYoinBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
			this.raitenYoinBeanList.add(raitenYoinBean);
		}
	}


	public BSWSSession getbSWSSession() {
		return bSWSSession;
	}

	public void setbSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public List<RaitenYoinBean> getRaitenYoinBeanList() {
		return raitenYoinBeanList;
	}

	public void setRaitenYoinBeanList(List<RaitenYoinBean> raitenYoinBeanList) {
		this.raitenYoinBeanList = raitenYoinBeanList;
	}

	public Integer getIntTaisyoSeq() {
		return intTaisyoSeq;
	}

	public void setIntTaisyoSeq(Integer intTaisyoSeq) {
		this.intTaisyoSeq = intTaisyoSeq;
	}

}